package com.jymj.service.impl;

import com.jymj.dao.RtInvestDao;
import com.jymj.entity.RtInvestEntity;
import com.jymj.entity.view.RtInvestTask;
import com.jymj.service.RtInvestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dl on 2017/10/15.
 */
@Service
public class RtInvestServiceImpl implements RtInvestService {

    @Autowired
    private RtInvestDao rtInvestDao;


    @Override
    public RtInvestEntity queryObject(Integer id) {
        return rtInvestDao.queryObject(id);
    }

    @Override
    public RtInvestEntity queryObjectByRefid(Integer id) {
        return rtInvestDao.queryObjectByRefid(id);
    }

    @Override
    public List<RtInvestTask> queryList(Map map) {
        return rtInvestDao.queryList(map);
    }

    @Override
    public List<RtInvestEntity> queryListWithChild(Map map) {
        return rtInvestDao.queryListWithChild(map);
    }

    @Override
    public void save(RtInvestEntity investEntity) {
        rtInvestDao.save(investEntity);
    }

    @Override
    public void update(RtInvestEntity investEntity) {
        rtInvestDao.update(investEntity);
    }

    @Override
    public void deleteById(int id) {
        rtInvestDao.delete(id);
    }

    @Override
    public void updateParentCost(RtInvestEntity investEntity) {
        Map map = new HashMap();
        map.put("costid", investEntity.getCostid());
        map.put("pid", investEntity.getPid());
        map.put("type",investEntity.getType());
        RtInvestEntity rtInvestEntity = rtInvestDao.queryRtInvestTask(map);
        //判断是否有上级
        if (rtInvestEntity == null || rtInvestEntity.getParentid() == 0) {
            return;
        }
        if (rtInvestEntity.getParentid() == null) {
            return;
        }
        //查找上级
        Map map1 = new HashMap();
        map1.put("costid", rtInvestEntity.getParentid());
        map1.put("pid", rtInvestEntity.getPid());
        map1.put("type",investEntity.getType());
        RtInvestEntity rtInvestEntity1 = rtInvestDao.queryObjectParent(map1);
        //上级有数据修改 无数据添加
        if (rtInvestEntity1 != null) {
            RtInvestEntity rtinvestEntity = new RtInvestEntity();
            rtinvestEntity.setPid(rtInvestEntity1.getPid());
            rtinvestEntity.setCostid(rtInvestEntity1.getCostid());
            rtinvestEntity.setType(rtInvestEntity1.getType());
            if (rtInvestEntity1.getId() == null) {
                rtInvestDao.saveRtCost(rtinvestEntity);
            }
            //计算
            rtInvestDao.updateParentCost(map1);
            this.updateParentCost(rtinvestEntity);
        }
    }

    @Override
    public void updateHjCost(RtInvestEntity investEntity) {
        Map<String, Object> map = new HashMap();
        map.put("pid", investEntity.getPid());
        map.put("parentid", 0);
        rtInvestDao.updateHjCost(map);
        rtInvestDao.updateHjCost1(map);
        rtInvestDao.updateHjCost2(map);
    }

    @Override
    public BigDecimal queryYzfCost(Integer pid, Integer itemid) {
        Map<String, Object> map = new HashMap();
        map.put("pid", pid);
        map.put("itemid", itemid);
        return rtInvestDao.queryYzfCost(map);
    }

    @Override
    public List<RtInvestEntity> queryRtInvestList(Map map) {
        return rtInvestDao.queryRtInvestList(map);
    }

    @Override
    public void saveRtInvest(RtInvestEntity rtInvestEntity) {
        rtInvestDao.savertInvest(rtInvestEntity);
    }

    @Override
    public void updateRtInvest(RtInvestEntity rtInvestEntity) {
        Map map = new HashMap();
        map.put("parentid", rtInvestEntity.getItemid());
        map.put("pid", rtInvestEntity.getPid());
        List<RtInvestEntity> rtInvestEntities = rtInvestDao.selectByItemid(map);
        if (rtInvestEntities == null || rtInvestEntities.isEmpty()) {
            rtInvestDao.updatertInvest(rtInvestEntity);
        }
    }

    @Override
    public void updateRtInvestCost(RtInvestEntity rtInvestEntity) {
        Map map = new HashMap();
        map.put("itemid", rtInvestEntity.getItemid());
        map.put("pid", rtInvestEntity.getPid());
        RtInvestEntity rtInvest = rtInvestDao.queryRtInvest(map);
        if (rtInvest != null && rtInvest.getParentid() != 0) {
            Map map1 = new HashMap();
            map1.put("parentid", rtInvest.getParentid());
            map1.put("itemid", rtInvest.getItemid());
            map1.put("pid", rtInvest.getPid());
            //计算预算、合同、四期金额
            rtInvestDao.updateHtCost(map1);

            //修改上级目录
            Map map2 = new HashMap<>();
            map2.put("itemid", rtInvest.getParentid());
            map2.put("pid", rtInvest.getPid());
            RtInvestEntity rtInvestEntities = rtInvestDao.queryRtInvest(map2);
            if (rtInvestEntities != null) {
                RtInvestEntity rtInvestEntity1 = new RtInvestEntity();
                rtInvestEntity1.setId(rtInvestEntities.getId());
                rtInvestEntity1.setParentid(rtInvestEntities.getParentid());
                rtInvestEntity1.setItemid(rtInvestEntities.getItemid());
                rtInvestEntity1.setPid(rtInvestEntities.getPid());
                this.updateRtInvestCost(rtInvestEntity1);
            }
        }
    }

    @Override
    public void updateZj(Integer pjid) {
        RtInvestEntity in=rtInvestDao.getTotal(pjid);
        if(in==null || in.getId()==null){
            rtInvestDao.saveRtCost(in);
        }
        //修改总计
        rtInvestDao.updateZj(pjid);
        //修改损益表
        //rtRaiseDao.updateXjlc(pjid);
    }

}
